package intro.databases.gtCarRental.ui.employee;

import intro.databases.gtCarRental.GTCRActivity;
import intro.databases.gtCarRental.R;
import intro.databases.gtCarRental.classes.Car;
import intro.databases.gtCarRental.classes.Location;

import java.util.ArrayList;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

public class ManageCars_ChangeCarLocation extends GTCRActivity {
	Spinner location, car, newLocation;
	EditText carType, color, seatingCapacity, transmissionType;
	
	ArrayList<Location> locs;
	ArrayList<Car> cars;
	int carIndex = 0;
	
	ArrayList<String> locNames;
	int nLocIndex = 0;
	
	@Override
	public void onCreate(Bundle b) {
		super.onCreate(b);
		setContentView(R.layout.change_car_location);
		setupViews();
	}

	private void setupViews() {
		location = (Spinner) findViewById(R.id.spinner_manageCars_changeCarLocation_location);
		car = (Spinner) findViewById(R.id.spinner_manageCars_changeCarLocation_car);
		newLocation = (Spinner) findViewById(R.id.spinner_manageCars_changeCarLocation_newLocation);

		carType = (EditText) findViewById(R.id.editText_manageCars_changeCarLocation_carType);
		color = (EditText) findViewById(R.id.editText_manageCars_changeCarLocation_color);
		seatingCapacity = (EditText) findViewById(R.id.editText_manageCars_changeCarLocation_seatingCapacity);
		transmissionType = (EditText) findViewById(R.id.editText_manageCars_changeCarLocation_transmissionType);

		locs = dbConn.getAllLocations();
		cars = dbConn.getCarsByLocation(locs.get(0).locationName);
		carIndex = 0;
		nLocIndex = 0;

		ArrayAdapter<String> lcAdapter = getLocationAdapter();
		lcAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		location.setAdapter(lcAdapter);
		location.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				cars = dbConn.getCarsByLocation(locs.get(arg2).locationName);
				System.out.println(cars);
				ArrayAdapter<String> cAdapter = getCarAdapter();
				cAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				car.setAdapter(cAdapter);
				
				ArrayAdapter<String> nlcAdapter = getNewLocationAdapter();
				nlcAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				newLocation.setAdapter(nlcAdapter);
			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {}
		});
		
		ArrayAdapter<String> cAdapter = getCarAdapter();
		cAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		car.setAdapter(cAdapter);
		car.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				try {
					carIndex = arg2;
					Car c = cars.get(arg2);
					carType.setText(c.type);
					color.setText(c.color);
					seatingCapacity.setText(""+c.seatingCapacity);
					transmissionType.setText(c.transmissionType);
				} catch (Exception e) {
					carType.setText("");
					color.setText("");
					seatingCapacity.setText("");
					transmissionType.setText("");
				}
			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {}
		});
		
		ArrayAdapter<String> nlcAdapter = getNewLocationAdapter();
		nlcAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		newLocation.setAdapter(nlcAdapter);
		newLocation.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				nLocIndex = arg2;
			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {}
		});
	}
	
	public void submitChanges(View v) {
		try {
			Car c = cars.get(carIndex);
			c.locationName = locNames.get(nLocIndex);
			dbConn.updateCar(c);
			setupViews();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	


	private ArrayAdapter<String> getLocationAdapter() {
		ArrayList<String> locNames = new ArrayList<String>();
		for(Location loc : locs)
			locNames.add(loc.locationName);
		
		ArrayAdapter<String> retVal = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,locNames);		
		return retVal;		
	}
	


	private ArrayAdapter<String> getCarAdapter() {
		ArrayList<String> carNames = new ArrayList<String>();
		try {
			for(Car car : cars)
				carNames.add(car.carModel);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		ArrayAdapter<String> retVal = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, carNames);		
		return retVal;		
	}
	


	private ArrayAdapter<String> getNewLocationAdapter() {
		locNames = new ArrayList<String>();
		try {
			for(Location loc : locs)
				if(!(loc.locationName.equals(cars.get(carIndex).locationName)))
					locNames.add(loc.locationName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		ArrayAdapter<String> retVal = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,locNames);		
		return retVal;		
	}
}
